package com.biz.primus.ms.payment.channel.allinpay.lang;

import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * DES
 *
 * @author JKLiues
 * @date 2017年11月13日
 */
public class DES {
    /**
     * 加密
     *
     * @param src      String
     * @param password String
     * @return String
     */
    public static String encrypt(String src, String password) {
        try{
            DESKeySpec desKey = new DESKeySpec(password.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(password.getBytes("UTF-8"));
            cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);
            byte[] bytes = cipher.doFinal(src.getBytes("UTF-8"));
            return Base64.encodeBase64String(bytes);
        }catch(Throwable e){
            e.printStackTrace();
        }
        return null;
    }


}